package com.wxxymaker.service.impl;

import com.wxxymaker.model.Model;
import com.wxxymaker.model.dao.StageCountDao;
import com.wxxymaker.model.dao.StageDao;
import com.wxxymaker.model.entity.MarkRecord;
import com.wxxymaker.model.entity.Shop;
import com.wxxymaker.model.entity.Stage;
import com.wxxymaker.model.entity.StageCount;
import com.wxxymaker.service.MarkRecordService;
import com.wxxymaker.service.ShopService;
import com.wxxymaker.service.StageCountService;
import com.wxxymaker.service.StageService;
import com.wxxymaker.utils.web.ShopUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Date;
import java.util.List;

/**
 * @Author: Soft
 * @Date: 2018/5/6 1:03
 * @Desc: 打分阶段业务层实现
 */
@Service
public class StageServiceImpl implements StageService {
    private final static Logger log = LoggerFactory.getLogger(StageServiceImpl.class);

    private final StageDao dao;

    private final MarkRecordService markRecordService;

    private final ShopService shopService;

    @Autowired
    public StageServiceImpl(StageDao dao, MarkRecordService markRecordService, ShopService shopService) {
        this.dao = dao;
        this.markRecordService = markRecordService;
        this.shopService = shopService;
    }

    @Override
    @Transactional
    public Stage add(Stage stage) {
        stage.setBeginTime(new Date());
        return dao.save(stage);
    }

    @Override
    @Transactional
    public Stage delete(Integer id) {
        Stage stage = dao.findOne(id);
        // 删除阶段前 解除与其相关的阶段统计
//        int i = stageCountDao.clearRefWithStage(id);
        if (null != stage) {
            dao.delete(stage);
        }
        return stage;
    }

    @Override
    @Transactional
    public boolean change(Stage stage) {
        Integer id = stage.getId();
        if (null != id && id > 0) {
            Stage extend = (Stage) dao.findOne(id).extend(stage);
            return null != dao.saveAndFlush(extend);
        }
        return false;
    }

    @Override
    public Stage getOne(Integer id) {
        return dao.findOne(id);
    }

    @Override
    public Page<Stage> page(Integer pageNo, Integer size) {
        if (pageNo < 1) {
            pageNo = 1;
        }
        Pageable pageable = new PageRequest(pageNo - 1, size,
                new Sort(Sort.Direction.DESC, "id"));
        return dao.findAll(pageable);
    }

    @Override
    @Transactional
    public boolean updateEndTime(Integer id) {
        return dao.setEndTime(id, new Date()) > 0;
    }

    @Override
    @Transactional
    public boolean addUserSum(Integer id) {
        return dao.addUserSum(id) > 0;
    }

    @Override
    public List<Stage> findAll() {
        return dao.findAll(new Sort(Sort.Direction.DESC, "id"));
    }

    @Override
    public List<Shop> getShops(Stage stage) {
        return shopService.findByIds(ShopUtil.getShopIdsArr(stage.getShopIds()));
    }

    @Override
    public long[] countStarNumForShop(Stage stage, Shop shop) {
        return new long[]{markRecordService.countForStar(stage, shop, MarkRecord.StarDim.BAD),
                markRecordService.countForStar(stage, shop, MarkRecord.StarDim.GOOD),
                markRecordService.countForStar(stage, shop, MarkRecord.StarDim.BETTER),
                markRecordService.countForStar(stage, shop, MarkRecord.StarDim.BEST)};
    }
}
